package com.tedu.startbooksstore.service.impl;

import com.tedu.startbooksstore.ex.ServiceException;
import com.tedu.startbooksstore.mapper.UserMapper;
import com.tedu.startbooksstore.pojo.dto.UserLoginDTO;
import com.tedu.startbooksstore.pojo.dto.UserRegDTO;
import com.tedu.startbooksstore.pojo.dto.UserUpdateDTO;
import com.tedu.startbooksstore.pojo.entity.User;
import com.tedu.startbooksstore.pojo.vo.UserListItemVO;
import com.tedu.startbooksstore.service.UserService;
import com.tedu.startbooksstore.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 用户业务逻辑实现层
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     * @param userRegDTO  : 需要注册的用户
     */
    @Override
    public void register(UserRegDTO userRegDTO) {
        log.debug("开始处理业务:用户注册");

        int countByUsername = userMapper.countByUsername(userRegDTO.getUsername());
        if (countByUsername>0){
            String message = "该用户名已经被注册,请更换用户名重新注册!";
            log.debug(message);
           throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int countByEmail = userMapper.countByEmail(userRegDTO.getEmail());
        if (countByEmail>0){
            String message = "该邮箱已经被注册,请更换邮箱重新注册!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        int countByPhone = userMapper.countByPhone(userRegDTO.getPhone());
        if (countByPhone>0){
            String message = "该手机号已经被注册,请更换手机号重新注册!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setEnable(1);
        user.setGmtCreate(LocalDateTime.now());
        user.setGmtModified(LocalDateTime.now());

        log.debug("即将注册的用户:{}",user);
        int insert = userMapper.insertOne(user);
        if (insert!=1){
            String message = "注册失败,服务器繁忙,请稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
        log.debug("用户{}已经添加到数据库",user);
    }

    /**
     * 用户登录
     * @param userLoginDTO  : 需要登录的用户
     */
    @Override
    public void login(UserLoginDTO userLoginDTO) {
        String pwd = userMapper.selectByUsername(userLoginDTO.getUsername());

        if (pwd==null){
            String message = "用户名错误,请重新输入用户名";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        if (!pwd.equals(userLoginDTO.getPassword())){
            String message = "密码错误,请重新输入密码";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        log.debug("用户名和密码正确,允许登录");
    }


    /**
     * 通过ID查询用户信息
     * @param id : 用户ID
     * @return : 用户信息
     */
    @Override
    public UserListItemVO selectById(Long id) {
        return userMapper.selectById(id);
    }

    /**
     *  查询用户列表
     * @return : 用户列表
     */
    @Override
    public List<UserListItemVO> list() {
        return userMapper.list();
    }

    /**
     * 通过ID修改用户信息
     * @param userUpdateDTO : 需要修改的用户信息
     */
    @Override
    public void updateById(UserUpdateDTO userUpdateDTO) {
        int update = userMapper.updateById(userUpdateDTO);
        if (update!=1){
            String message = "修改失败,服务器繁忙,请稍后再试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }
}
